package ea977;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author antonio
 */
public class criaXML_2 {

    public static File fileXML2;
    
    public void criaXML_2(){}
        public File setaArquivoXMLDeputado(String Deputado){
        
            File fileXML1 = new File (Deputado);
            return fileXML1;
    }
    public File setaArquivoXMLSenador(String Senador){
        
            File fileXML1 = new File (Senador);
            return fileXML1;
    } 
        
            //o que será passado como parâmetro será é o conteúdo das tags
            public void novoXML(String fileXML2,String Content_estado, String Content_nome, String Content_partido) throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                File xml = new File (fileXML2);
                criaXML_2.fileXML2 = xml;
		// root elements
		Document doc = docBuilder.newDocument();
		Element rootElement = doc.createElement("Politico");
		doc.appendChild(rootElement);
 
		// PL elements
		//Element TAG_Pol = doc.createElement("Pol");
		//rootElement.appendChild(TAG_Pol);
                /*CASO SEJA NECESSARIO ADICIONAR UM ATRIBUTO*/
		// set attribute to staff element
		//Attr attr = doc.createAttribute("id");
		//attr.setValue("1");
		//staff.setAttributeNode(attr);
 
		// shorten way
		// staff.setAttribute("id", "1");
 
		// firstname elements
		//Element ESTADO = doc.createElement("ESTADO");
		//ESTADO.appendChild(doc.createTextNode(Content_estado));
		//TAG_Pol.appendChild(ESTADO);
 
				// salary elements
		//Element LINK = doc.createElement("LINK");
		//LINK.appendChild(doc.createTextNode(Content_link));
		//TAG_Pol.appendChild(LINK);
 
		// write the content into xml file
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(criaXML_2.fileXML2);
 
		// Output to console for testing
		 //StreamResult result = new StreamResult(System.out);
 
		transformer.transform(source, result);
    }
    //o conteúdo das tags será passado como parâmetro para construir o XML.
            public void insereNovoNOXML(String Content_estado,String Content_partido, String Content_nome, 
                    String Content_endereco, String Content_email, String Content_telefone)
                    throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerException{
                //File fXmlFile = new File("c:\\users/antonio/Documents/filenew.xml");
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(criaXML_2.fileXML2);
                doc.getDocumentElement().normalize();
                
                Element TAG_PL = doc.createElement("Pol");
                
                Element root = doc.getDocumentElement();  
                root.appendChild(TAG_PL);
                
                		// PL elements
		//Element TAG_PL = doc.createElement("PL");
		//rootElement.appendChild(TAG_PL);
                /*CASO SEJA NECESSARIO ADICIONAR UM ATRIBUTO*/
		// set attribute to staff element
		//Attr attr = doc.createAttribute("id");
		//attr.setValue("1");
		//staff.setAttributeNode(attr);
 
		// shorten way
		// staff.setAttribute("id", "1");
 
		// firstname elements
		Element ESTADO = doc.createElement("ESTADO");
		ESTADO.appendChild(doc.createTextNode(Content_estado));
		TAG_PL.appendChild(ESTADO);
                
                Element PARTIDO = doc.createElement("PARTIDO");
		PARTIDO.appendChild(doc.createTextNode(Content_partido));
		TAG_PL.appendChild(PARTIDO);
 
                Element NOME = doc.createElement("NOME");
		NOME.appendChild(doc.createTextNode(Content_nome));
		TAG_PL.appendChild(NOME);
                
                Element ENDERECO = doc.createElement("ENDERECO");
		ENDERECO.appendChild(doc.createTextNode(Content_endereco));
		TAG_PL.appendChild(ENDERECO);
                
                Element EMAIL = doc.createElement("EMAIL");
		PARTIDO.appendChild(doc.createTextNode(Content_email));
		TAG_PL.appendChild(EMAIL);
 
                Element TELEFONE = doc.createElement("TELEFONE");
		TELEFONE.appendChild(doc.createTextNode(Content_telefone));
		TAG_PL.appendChild(TELEFONE);
                
 		// salary elements
		//Element LINK = doc.createElement("LINK");
		//LINK.appendChild(doc.createTextNode(Content_link));
		//TAG_PL.appendChild(LINK);
                
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(criaXML_2.fileXML2);
 
		// Output to console for testing
		 //StreamResult result = new StreamResult(System.out);
 
		transformer.transform(source, result);
		//rootElement.appendChild(TAG_PL);
            }
            
            
}

